package com.pengchuang.ACS.admin.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pengchuang.ACS.admin.boot.dao.SysUserRoleDao;
import com.pengchuang.ACS.admin.boot.entity.SysRoleEntity;
import com.pengchuang.ACS.admin.boot.entity.SysUser;
import com.pengchuang.ACS.admin.boot.entity.SysUserRoleEntity;
import com.pengchuang.ACS.admin.boot.service.ISysUserService;
import com.pengchuang.ACS.admin.boot.service.SysRoleService;
import com.pengchuang.ACS.admin.boot.service.SysUserRoleService;
import com.pengchuang.ACS.common.constant.SecurityConstants;
import com.pengchuang.ACS.common.utils.PageUtils;
import com.pengchuang.ACS.common.utils.R;
import com.pengchuang.ACS.common.utils.ResultCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Map;

/**
 * @description:
 * @projectName: AccessControlSystem
 * @see: com.pengchuang.ACS.admin.boot.controller
 * @author: pc
 * @createTime: 2023/3/14 15:38
 * @version: 1.0
 */
@RestController
@RequestMapping("/admin/users")
@Slf4j
@RequiredArgsConstructor
public class SysUserController {

	private final ISysUserService iSysUserService;

	private final SysRoleService roleService;

	private final SysUserRoleService service;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) throws UnsupportedEncodingException {
		return iSysUserService.queryPageWithAuth(params);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{username}")
	public R info(@PathVariable("username") String username){
		SysUser user = iSysUserService.getOne(new QueryWrapper<SysUser>().eq("username", username));
		user.setPassword("");
		return R.ok().put("result", user);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestParam SysUser user){
		user.setUsername(user.getPhone());
		SysRoleEntity role = roleService.getBaseMapper()
						.selectOne(new QueryWrapper<SysRoleEntity>().eq("name", user.getRoleName()));
		if (role == null) {
			return R.error(ResultCode.PARAM_VALID_FAIL);
		}
		iSysUserService.saveOrUpdate(user);
		// 调用feign保存 用户图片的相关信息

		service.save(SysUserRoleEntity.builder().userId(user.getId()).roleId(role.getId()).build());
		return R.ok();
	}

	/**
	 * 用户门禁状态
	 */
	@RequestMapping("/is_open")
	public R update(@RequestParam Long id, @RequestParam Integer isOpen){
		if (isOpen != 0 && isOpen != 1) {
			return R.error(ResultCode.PARAM_VALID_FAIL);
		}
		SysUser byId = iSysUserService.getById(id);
		byId.setIsOpen(isOpen);
		iSysUserService.updateById(byId);

		return R.ok("用户更新完成");
	}
	/**
	* 重置密码
	*/
	@RequestMapping("/psw")
	public R rePassword(@RequestParam Long id){
		// TODO 重置密码
		return R.ok("重置密码完成");
	}


	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestParam Integer ids){
		iSysUserService.removeById(ids);

		return R.ok();
	}

}
